<?php
/**
 * 
 * Enter description : thoa tac voi db de cai dat he thong 
 * @author admin888 <doan.hut@gmail.com >
 *
 */
class Model_Caidat extends Zend_Db_Table
{
    protected $_thanhvien = "thanhvien"; 
	protected $_cauhinh = "cau_hinh_user";
	protected $_lien_he = "lien_he";
	protected $_nhom_lien_he = "nhom_lien_he";
	protected $_thu_gui = "thu_gui";
	protected $_thu_mau = "thu_mau";
	protected $_dinh_kem = "dinh_kem";
	
	
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :lay thon tin cua user
	 * @param unknown_type $iduser
	 */
	public function gethongtin($iduser)
	{
		$tbconfig = "cau_hinh_user";
		$sql = $this->_db->select()
						 ->from($tbconfig)
						 ->where('uid =?',$iduser);
		$query = $this->_db->fetchRow($sql);
		return $query;				 
	}
	/**
	 * admin888 <paven@gmail.com>
	 * Enter description : Lấy thông tin cá nhân của user 
	 * @param unknown_type $iduser
	 */
	public function getthongtincanhan($iduser)
	{
		$tbthanhvien = "thanhvien";
		$sql = $this->_db->select()
						  ->from($tbthanhvien)
						  ->where('uid =?',$iduser);
		$query = $this->_db->fetchRow($sql);
	return $query;							  
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description get thông tin thành viên trong bang cau hinh user và bang thanh viên
	 * @param unknown_type $uid
	 */
	public function getthongsotheoid($uid)
	{
		$sql = $this->_db->select($this->_cauhinh)
						 ->from($this->_cauhinh)
						 ->join($this->_thanhvien, $this->_thanhvien.'.uid = '.$this->_cauhinh.'.uid','name')
						 ->where($this->_cauhinh.'.uid = ?',$uid);
					 
		$query = $this->_db->fetchRow($sql)	;
	 return $query;				 	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description update thong tin cai dat emal
	 * @param unknown_type $post
	 */
	public function updatecaidatemail($post)
	{
		$date = new Zend_Date();
		$tbconfig = "cau_hinh_user";
		$data = array(
					'SMTP_server' => trim($post['sptpserver']),		
					'SMTP_port' => trim($post['sptport']),		
					'user_name_email' => trim($post['unamesmpt']),		
					'pass_email' =>base64_encode(trim($post['passsmpt'])),
					'loai_mahoa' => trim($post['loaimahoa']),		
					'ngay_sua' => $date,
                    'nguoi_sua' =>$post['idnddn']		
					);
					$where ="chid = ".$post['chid'];
		$query = $this->_db->update($tbconfig, $data,$where);
	  return $query;			
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :Update thuong tin ca nhan cua onne user
	 * @param unknown_type $data
	 */
	public function getupdatethongtinchung($data)
	{
		$date = new Zend_Date();
		$tbuser = "thanhvien";
		$array  = array(
						'name'	=> $data['ho_va_ten'],
						'uname'	=> $data['ten_dang_nhap'],
						'email'	=> $data['email'],
						'phone'	=> $data['dien_thoai'],
						'address'	=> $data['dia_chi'],
						'ngay_sua' => $date,	
                        'nguoi_sua' => $data['idnddn']
						);
						$where = "uid = ".$data['uid'];
		$query = $this->_db->update($tbuser, $array,$where);
	 return $query;	 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :update pass
	 * @param unknown_type $dulieu
	 */
	public function updatepass($dulieu)
	{
		$date = new Zend_Date();
		$tbuser ="thanhvien";
		$aray = array(
				'pass'      => md5($dulieu['pass']),
                'nguoi_sua' => $dulieu['iddn'],
				'ngay_sua'  => $date
				);
		$where = "uid = ".$dulieu['id'];		
		$query = $this->_db->update($tbuser, $aray, $where);
	 return $query;		
	}
	
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : ket noi smtp 
	 * @param unknown_type $thongso
	 */
	public function getketnoi($thongso)
	{
		$callemal = new Zend_Mail_Transport_Smtp
				( trim($thongso['smtpserver']),
										array (
												'auth' => 'login',
												'username' => trim ($thongso['tendangnhap']),
												'password' => trim($thongso['matkhau']),
												'ssl' => trim($thongso['mahoa']),
												'port' => trim($thongso['smtpport'])
												)
				 );		 
		
        Zend_Mail::setDefaultTransport ($callemal);
         $mail = new Zend_Mail ( 'UTF-8' );
         $mail->setBodyHtml (('Test kết nối email'));
         $mail->addTo (trim ($thongso['tendangnhap']));
         $mail->setSubject ('Email Maneger');
         $mail->setFrom ($thongso['tendangnhap']);
         //$ketnoi = $mail->send();
		if (!$mail->Send()){   		  
		 	return 0; 
		 }else{
            return  1;
           
		 }
	    // try {$mail->send();  }catch (Exception $e) {   $data->error = $e->getMessage();}
	     
		 
	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :lấy toàn bộ thành viên
	 */
	public function getlisthanhvien()
	{
		$tbthanhvien = "thanhvien" ;
		$sql = $this->_db->select()
					->from($tbthanhvien);
		$query = $this->_db->fetchAll($sql);
	 return $query;				
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :lấy toàn bộ thành viên tim liem 
	 */
	public function getlisthanhvientimkiem($timkiem)
	{
		$tbthanhvien = "thanhvien" ;
		$sql = $this->_db->select()
					->from($tbthanhvien)
                    ->where('name LIKE ?', '%'.$timkiem.'%');      
		$query = $this->_db->fetchAll($sql);
	 return $query;				
	}    
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :thuc thi khoa thanh viên
	 * @param unknown_type $dulieu
	 */
	public  function blocktv($dulieu)
	{
		$date = new Zend_Date();
		$arr = array(
				'su_dung' => $dulieu['thongso'] ,
				'ngay_sua' => $date, 
				'nguoi_sua' => $dulieu['iddn'] 
		);
		$where = "uid = ".$dulieu['id'];
		$ud = $this->_db->update($this->_thanhvien,$arr, $where);	
	 return $ud;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : thuc thu thi add i ban ghi cho bang cau_hinh_user 
	 * @param unknown_type $dulieu
	 */
	public function kichhoatthanhviensms($dulieu)
	{
		$date = new Zend_Date();
		//add ban ghi vao bang cai hinh
		$arr = array(
			'uid' => $dulieu['id'],
			'loai_mahoa' => 'ssl',
			'ngay-tao' => $date,
			'ngay_sua' => $date,
			'nguoi_sua' => $dulieu['iddn'],
			'nguoi_tao' => $dulieu['iddn']
		
		);
		$add = $this->_db->insert($this->_cauhinh, $arr);
		return $add;
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :thuc hien update khich hoat
	 * @param unknown_type $dulieu
	 */
	public  function mokhoakichhoat($dulieu)
	{
		$date = new Zend_Date();
		$arr = array(
				'kichhoat' => 1 ,
				'ngay_sua' => $date, 
				'nguoi_sua' => $dulieu['iddn'] 
		);
		$where = "uid = ".$dulieu['id'];
		$ud = $this->_db->update($this->_thanhvien,$arr, $where);	
	 return $ud;	
	}
    /**
     * admin888 <doan.hut@gmail.com>
     * Ajax tim kiem thanh vien 
     * 
     */ 
     public function gettimkiemthanhvien($data)
     {
       $tukhoa =  $data['queryString'];
		$tbthanhvien = "thanhvien" ;
		$sql = $this->_db->select()
					->from($tbthanhvien)
                    ->where('name LIKE ?', '%'.$tukhoa.'%')
                    ->limit(10);      
		$query = $this->_db->fetchAll($sql);
        
      return $query;                   
     }
     	/**
	 * admin888 <paven@gmail.com>
	 * Enter description : Lấy thông tin cá nhân của user  fe
	 * @param unknown_type $iduser
	 */
	public function timkiemone($iduser)
	{
		$tbthanhvien = "thanhvien";
		$sql = $this->_db->select()
						  ->from($tbthanhvien)
						  ->where('uid =?',$iduser);
		$query = $this->_db->fetchAll($sql);
	return $query;							  
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : updatechu ky
	 * @param unknown_type $data
	 */
	public function updatechuky($data)
	{
		if($data['sudungchuky'] == 'on'){
			$sudung = 1;
		}else{
			$sudung = 0;
		}
		$date = new Zend_Date();
		$arr = array(
			'chu_ky'   			=> stripslashes($data['chuky']),
			'su_dung_chu_ky'	=> $sudung,
			'ngay_sua'			=> $date,
			'nguoi_sua'			=> $data['idnddn'] 
			
		);
		$where = "uid = ".$data['uid'];
		$query  = $this->_db->update($this->_cauhinh, $arr,$where);
	return  $query;	
	}
// ============================= Dức 
	/**
     * admin888 <doan.hut@gmail.com>
     * Enter description :xoa thanh vien
     * @param unknown_type $idtv
     */
    public function deletethanhvien($idtv)
    {
    	$where = 'uid ='.$idtv;
    	
    	$xoatv = $this->_db->delete($this->_thanhvien,$where);
    	$xoach = $this->_db->delete($this->_cauhinh,$where);
    	$xoalh = $this->_db->delete($this->_lien_he,$where);
    	$xoanlh = $this->_db->delete($this->_nhom_lien_he,$where);
    	$xoatg = $this->_db->delete($this->_thu_gui,$where);
    	$xoatm = $this->_db->delete($this->_thu_mau,$where);
    	$xoafdk = $this->_db->delete($this->_dinh_kem,$where);
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description :xóa nhiều thanh vien
     * @param unknown_type $mangid
     */
    public function deletealltv($mangid)
    {
    	$arrayid = explode(',', $mangid);
    	for ($i =0;$i< count($arrayid);$i++){
    		$idtv = $arrayid[$i];
    		//sql xóa
    		$where = 'uid = '.$idtv;
    		$xoa = $this->_db->delete($this->_thanhvien,$where);
    		$xoach = $this->_db->delete($this->_cauhinh,$where);
    	    $xoalh = $this->_db->delete($this->_lien_he,$where);
    	    $xoanlh = $this->_db->delete($this->_nhom_lien_he,$where);
    	    $xoatg = $this->_db->delete($this->_thu_gui,$where);
    	    $xoatm = $this->_db->delete($this->_thu_mau,$where);
    	    $xoafdk = $this->_db->delete($this->_dinh_kem,$where);
    	}
    }
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : Xoa file trong thu muc 
	 * @param unknown_type $link
	 */
	public function xoafilecuathumuc($link)
	{
		$xoa = unlink($link);		
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : lay file da lu 
	 * @param unknown_type $idt
	 */
	
	public function getalllinkfile($idtv)
	{
		$slq = $this->_db->select()
						 ->from($this->_dinh_kem)
						 ->where($this->_dinh_kem.'.uid =?',$idtv);
						 
		$query = $this->_db->fetchAll($slq);
	return $query;					 		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description Tong file cua thanh vien 
	 * @param unknown_type $uid
	 */
	public function getongfilethanhvien($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_dinh_kem, array('COUNT(*) AS tongfile'))
						 ->where('uid ='.$uid);
		return  $this->_db->fetchOne($sql);				 
						 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : get thong so da doc thong bao hay chưa 
	 * @param unknown_type $uid
	 */
	public function  getthongbao($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_cauhinh)
						 ->where('uid ='.$uid);
			return  $this->_db->fetchRow($sql);			 
						 		
	}
	public function  docthongbaobd($uid)
	{
		$arr = array(
			'thong_bao' 		=>0,
		);
		$where = "uid = ".$uid;
	 return $this->_db->update($this->_cauhinh, $arr,$where);	
	}
	

	
}